package ru.papergames.battleserver.model.battle.npc;

import ru.papergames.battleserver.model.battle.BallColor;
import ru.papergames.battleserver.model.battle.map.BattleMap;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Alexander Troshanin
 */
public class NpcDataContainer {

    private BallColor color;

    private NpcBattleMap map;

    private Set<NpcBall> allNpcBalls = new HashSet<NpcBall>();

    private Set<NpcBall> unlockedNpcBalls = new HashSet<NpcBall>();

    private Set<NpcBall> blockedNpcBalls = new HashSet<NpcBall>();

    private Set<NpcLine> npcLines = new HashSet<NpcLine>();

    private Set<NpcLine> unlockedNpcLines = new HashSet<NpcLine>();

    private Set<NpcLine> blockedNpcLines = new HashSet<NpcLine>();

    private AtomicInteger packetsCountToBeProcessed = new AtomicInteger(0);

    public NpcDataContainer(BattleMap battleMap) {
        map = new NpcBattleMap(battleMap.getWidth(), battleMap.getHeight());
    }

    public NpcBattleMap getMap() {
        return map;
    }

    public Set<NpcBall> getAllNpcBalls() {
        return allNpcBalls;
    }

    public Set<NpcLine> getNpcLines() {
        return npcLines;
    }

    public Set<NpcLine> getUnlockedNpcLines() {
        return unlockedNpcLines;
    }

    public Set<NpcLine> getBlockedNpcLines() {
        return blockedNpcLines;
    }

    public void addLine(NpcLine line) {
        npcLines.add(line);
    }

    public void removeLine(NpcLine line) {
        npcLines.remove(line);
        blockedNpcLines.remove(line);
        unlockedNpcLines.remove(line);
    }

    public void addNpcBall(NpcBall npcBall) {
        allNpcBalls.add(npcBall);
    }

    public Set<NpcBall> getUnlockedNpcBalls() {
        return unlockedNpcBalls;
    }

    public Set<NpcBall> getBlockedNpcBalls() {
        return blockedNpcBalls;
    }

    public void setNpcBallBlockStatus(NpcBall npcBall, boolean blocked) {
        npcBall.setBlocked(blocked);
        if (blocked) {
            unlockedNpcBalls.remove(npcBall);
            blockedNpcBalls.add(npcBall);
        } else {
            blockedNpcBalls.remove(npcBall);
            unlockedNpcBalls.add(npcBall);
        }
    }

    public void setNpcLineBlockStatus(NpcLine line, boolean blocked) {
        if (blocked) {
            unlockedNpcLines.remove(line);
            blockedNpcLines.add(line);
        } else {
            blockedNpcLines.remove(line);
            unlockedNpcLines.add(line);
        }
    }

    public void removeNpcBall(NpcBall npcBall) {
        allNpcBalls.remove(npcBall);
        blockedNpcBalls.remove(npcBall);
        unlockedNpcBalls.remove(npcBall);
    }

    public int getPacketsCountToBeProcessed() {
        return packetsCountToBeProcessed.get();
    }

    public void incPacketsCountToBeProcessed() {
        packetsCountToBeProcessed.incrementAndGet();
    }

    public void decPacketsCountToBeProcessed() {
        packetsCountToBeProcessed.decrementAndGet();
    }
}
